/**
 * Created by rachel.fan on 2016-6-28.
 */
//var tcfModule='';

var newModule=' <div  class="bnModule clearfix"><div class="zb-module-group" id="zbGroup2">'+
    '<div class="zb-header"><span><b>模块一：</b> <input type="text" value="新建模块1"/></span><span class="fr"><a href="#" class="close-x icon-close icon"></a><a href="#" class="pickUp icon-arrow-up2 icon"></a> </span></div>'+
    '<div class="zb-body"><div class="bn-content"><div class="bn-left mdLeft fl"><p class="mdTit">请选择层级样式</p><div class="zbSelect">'+
    '<div class="radio-group"><div class="radione  checked"><input type="radio" name="radiobj" value="a"><span>样式一（一层指标结构</span></div>'+
   '<div class="radione radio2"><input type="radio" name="radiobj" value="a"><span>样式二（二层指标结构）</span></div>'+
    '<div class="radione radio3"><input type="radio" name="radiobj" value="a"><span>样式三（三层指标结构）</span></div></div>'+
    '<div class="bn-style1  clearfix"><div class="style1-content style1"><ul><li><input type="text" value="一级指标" readonly="readonly"/><em class="close-x icon-close icon"></em></li>'+
    '<li> <input type="text" value="一级指标" readonly="readonly"/><em class="close-x icon-close icon"></em> </li> </ul></div></div>'+
    '<div class="bn-style2  clearfix"><div class="style2-content styleGroup"><ul><li class="module-item"><h6><input type="text" value="一级指标" readonly="readonly"/>'+
    '<em class="close-x icon-close icon"></em></h6><ul><li class="bm-item"><input type="text" value="二级指标" data-value="二级指标" readonly="readonly"><em class="close-x icon-close icon"></em></li>'+
    '<li class="bm-item marAdd"><input type="text" value="二级指标" data-value="二级指标" readonly="readonly"><em class="close-x icon-close icon"></em> </li><li class="bm-item">'+
    '<input type="text" value="二级指标" data-value="二级指标" readonly="readonly"><em class="close-x icon-close icon"></em></li><li class="bm-item marAdd">'+
    '<input type="text" value="二级指标" data-value="二级指标" readonly="readonly"><em class="close-x icon-close icon"></em></li></ul></li></ul></div></div>'+
    '<div class="bn-style3 clearfix"><div class="style3-content styleGroup"><ul><li class="module-item"><h6><input type="text" value="一级指标" readonly="readonly"/><em class="close-x icon-close icon"></em></h6>'+
    '<ul><li class="bm-item"><input type="text" value="二级指标" data-value="二级指标" readonly="readonly"><em class="close-x icon-close icon"></em></li><li class="bm-item marAdd">'+
    '<input type="text" value="二级指标" data-value="二级指标" readonly="readonly"><em class="close-x icon-close icon"></em></li><li class="bm-item item3 item-dev"><input type="text" value="三级指标" data-value="三级指标" readonly="readonly">'+
    '<em class="close-x icon-close icon"></em></li><li class="bm-item item3 marAdd"><input type="text" value="三级指标" data-value="三级指标" readonly="readonly"><em class="close-x icon-close icon"></em> </li> </ul> </li> </div> </div> </div> </div>'+
    '<div class="bn-right mdRight fl"><p class="mdTit">指标</p><div class="zbShow"><div class="zb-style1 style1"><ul><li class="zbAdd add-mdGroup"><b>点击此创建新指标</b></li></ul></div>'+
    '<div class="zb-style2"><ul><li class="zbAdd add-mdGroup"><b>点击此创建一级新指标</b></li></ul></div><div class="zb-style3"><ul><li class="zbAdd add-mdGroup">'+
    '<b>点击此创建一级新指标</b></li></ul></div></div></div></div></div></div></div>';



$(function(){
    //TCF建模和全新建模切换
    $(".styleSelect a").click(function(){
        var ind=$(this).index();
        $(".wrapper>div").eq(ind).show().siblings().hide();
        $(this).addClass("sel").siblings().removeClass("sel");
    }).eq(0).click();


    //TCF建模和全新建模关闭
    $(".TCF-wrapper .zb-header .close-x").click(function(){
        $(this).parent().parent().parent().parent().hide();
        $(".styleSelect a").eq(0).removeClass("sel");
    });

    $(document).on('click','.brandNew-wrapper .zb-header .close-x', function(){
        $(this).parent().parent().parent().parent().remove();
        $(".styleSelect a").eq(1).removeClass("sel");
    });

    $(document).on('click','.zb-header .pickUp', function(){
        if($(this).hasClass("icon-arrow-up2")){
            $(this).parent().parent().siblings(".zb-body").hide();
            $(this).removeClass("icon-arrow-up2").addClass("icon-arrow-down2");
        }else{
            $(this).parent().parent().siblings(".zb-body").show();
            $(this).removeClass("icon-arrow-down2").addClass("icon-arrow-up2");
        }
    });



    //全新建模新建大模块
    $(document).on('click','.add-zbGroup', function(){
        var len=$(this).siblings("div").length;
        if(len>=1){
            var id=$(this).prev().attr("id");
            var aft=parseInt(id.substring(8,9))+1;
            $(this).before('<div class="bnModule" id="bnModule'+aft+'">'+newModule+'</div>');
            $('#bnModule'+aft).find(".zb-header input").val('新建模块'+aft);
            zbBuilt('bnModule'+aft);
        }else{
            $(this).before('<div class="bnModule" id="bnModule1">'+newModule+'</div>');
            $('#bnModule1').find(".zb-header input").val('新建模块1');
            zbBuilt('bnModule1');
        }
    });

    // TCF建模新建指标组
    $('.TCF-wrapper .add-out-tcf b').on('click', function () {
        var nzbleng = $('.add-from-out').length + 1;
        var newZBkuai = '<li " class="module-item add-from-out"><h6><input type="text" value="新建标题' + nzbleng + '"><em class="total-x close-x icon-close icon"></em></h6><ul class="clearfix tr-itemGroup">';
        newZBkuai += '<li class="add-in-tcf"><b>' + measureAddTip + '</b></li></ul></li>';
        $(this).parent().before(newZBkuai);

        //新建小指标
        $('.tr-itemGroup>li.add-in-tcf').on('click', function () {
            $(this).before('<li><input type="text" value="新建指标" data-value="新建指标"><em class="close-x icon-close icon"></em></li>');
            $(this).prev().find('em.close-x').on('click', function(){
                $(this).parent().remove();
            });
            optionSubAlign();
        });

        //新建指标组关闭
        $(this).closest('li').prev().find('em.total-x:last').on('click', function () {
            $(this).closest('li').find('em.close-x:not(".total-x")').click();
            $(this).closest('li').remove();
        });

        $('.add-from-out:last').find('em.close-x:not(".total-x")').on('click', function () {
            //新建指标组小指标关闭
            var groupid = $(this).closest('li').attr('data-groupid');
            var mcid = $(this).closest('li').attr("id");
            $('#' + id + ' .tl-group>li[data-groupid="' + groupid + '"] > .tl-itemGroup > #' + mcid).show();
            $(this).parent('li').remove();
        });

        var id = $(this).closest('.zb-module-group').attr('id');

        OnlineMeasure.initLastDrop(id);
    });

    $(window).resize(function() {
        optionSubAlign();
    });

});